package com.laf.framework.web.service;


import com.laf.common.core.domain.entity.MobileUser;
import com.laf.common.core.domain.model.LoginMobileUser;
import com.laf.common.exception.ServiceException;
import com.laf.common.utils.StringUtils;
import com.laf.mobile.service.IMobileUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

@Service("mobileUserDetailsServiceImpl")
public class MobileUserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private IMobileUserService mobileUserService;

    private static final Logger log = LoggerFactory.getLogger(UserDetailsServiceImpl.class);

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        MobileUser mobileUser = mobileUserService.selectUserByUserName(username);
        System.out.println("这是移动端用户：" + mobileUser);
        if (StringUtils.isNull(mobileUser)) {
            log.info("登录用户：{} 不存在.", username);
            throw new ServiceException("登录用户：" + username + " 不存在");
        }

        return createLoginUser(mobileUser);
    }

    public UserDetails createLoginUser(MobileUser mobileUser) {
        return new LoginMobileUser(mobileUser.getUserId(), mobileUser);
    }

}














